package com.ruoyi.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.exam.domain.ExamBankQuestion;
import com.ruoyi.exam.vo.ExamBankQuestionVo;
import com.ruoyi.exam.vo.ExamQuestionWithSortVo;
import com.ruoyi.exam.mapper.ExamBankQuestionMapper;
import com.ruoyi.exam.service.IExamBankQuestionService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 题库题目关联Service业务层处理
 *
 * @author Tellsea
 * @date 2025-05-30
 */
@Service
public class ExamBankQuestionServiceImpl extends ServiceImpl<ExamBankQuestionMapper, ExamBankQuestion> implements IExamBankQuestionService {

    // @Autowired
    // private ExamUserBankProgressMapper examUserBankProgressMapper;

    // @Autowired
    // private ExamUserPracticeRecordMapper examUserPracticeRecordMapper;

    @Override
    public TableDataInfo<ExamBankQuestionVo> queryList(ExamBankQuestionVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<ExamBankQuestionVo> queryAll(ExamBankQuestionVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public ExamBankQuestionVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    public List<ExamQuestionWithSortVo> getQuestionsByBankId(Long userId,Long bankId) {
        // UserBankProgressAndRecordVo result = new UserBankProgressAndRecordVo();
        
        // 查询进度信息
        // ExamUserBankProgressVo progress = examUserBankProgressMapper.getByUserIdAndBankId(userId, bankId);
        // result.setProgress(progress);
        // result.setHasProgress(progress != null);
        
        // 查询答题记录
        // List<ExamUserPracticeRecordVo> records = examUserPracticeRecordMapper.getByUserIdAndBankId(userId, bankId);
        // result.setPracticeRecords(records);
        // result.setHasRecords(records != null && !records.isEmpty());
         
        return this.baseMapper.getQuestionsByBankId(bankId);
    }

    @Override
    public List<Long> getQuestionIdsByBankId(Long bankId) {
        return this.baseMapper.getQuestionIdsByBankId(bankId);
    }

    @Override
    public ExamQuestionWithSortVo getQuestionBySortOrder(Long bankId, Long sortOrder) {
        return this.baseMapper.getQuestionBySortOrder(bankId, sortOrder);
    }

    @Override
    public ExamQuestionWithSortVo getNextQuestion(Long bankId, Long currentSortOrder) {
        return this.baseMapper.getNextQuestion(bankId, currentSortOrder);
    }

    @Override
    public ExamQuestionWithSortVo getFirstQuestion(Long bankId) {
        return getQuestionBySortOrder(bankId, 1L);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reorderQuestions(Long bankId, List<Long> questionIds) {
        // 重新分配排序号
        for (int i = 0; i < questionIds.size(); i++) {
            Long questionId = questionIds.get(i);
            Long newSortOrder = (long) (i + 1);
            
            // 更新排序号
            LambdaUpdateWrapper<ExamBankQuestion> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ExamBankQuestion::getBankId, bankId)
                         .eq(ExamBankQuestion::getQuestionId, questionId)
                         .set(ExamBankQuestion::getSortOrder, newSortOrder);
            this.update(updateWrapper);
        }
    }
}
